package fr.insee.solr.model.param;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;


public class SolrFacetParamsBuilder {
	
	
	
	
	private List<SolrRequestParam.Param> params = new ArrayList<SolrRequestParam.Param>();

	
	public static SolrFacetParamsBuilder create(String name){
		SolrFacetParamsBuilder sf = new SolrFacetParamsBuilder();
		
		return sf;
	}
	
	public SolrFacetParamsBuilder add(String name, Object value){
		if(name != null && value != null){
			SolrRequestParam.Param p = new SolrRequestParam.Param(name, value.toString());
			params.add(p);
		}
		return this;
	}
	
	public List<SolrRequestParam.Param> build(){
		return params;
	}
	
	public static  List<SolrRequestParam.Param> buildFacetField(SolrFacetParams params){
		String name = params.getField();
		return SolrFacetParamsBuilder.create(name)
			.add("facet.field", name)
			.add(fill("f.<NAME>.facet.missing",name), params.getMissing())
			.add(fill("f.<NAME>.facet.method",name), params.getMethod())
			.add(fill("f.<NAME>.facet.mincount",name), params.getMincount())
			.add(fill("f.<NAME>.facet.offset",name), params.getOffset())
			.add(fill("f.<NAME>.facet.limit",name), params.getLimit())
			.add(fill("f.<NAME>.facet.sort",name), params.getSort()).build();
	}
	
	public static  List<SolrRequestParam.Param> buildFacetQuery(SolrFacetParams params){
		String name = params.getQuery();
		// TODO
		return SolrFacetParamsBuilder.create(name)
			.add("facet.field", name)
			.build();
	}
	
	public static  List<SolrRequestParam.Param> buildFacetDate(SolrFacetParams params){
		String name = params.getDate();
		// TODO
		return SolrFacetParamsBuilder.create(name)
			.add("facet.field", name)
			.build();
	}
	
	public static  List<SolrRequestParam.Param> buildFacetRange(SolrFacetParams params){
		String name = params.getRange();
		// TODO
		return SolrFacetParamsBuilder.create(name)
			.add("facet.field", name)
			.build();
	}
	
	private static String fill(String param,String name){
		return StringUtils.replaceOnce(param, "<NAME>", name);
	}
}
